#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "abcrypto.h"

static struct abcrypto_private_ctx g_abcrypto_private_ctx;

int register_instance(struct abcrypto_instance *instance)
{
	uint16_t i;
	struct abcrypto_instance *cur;
	if (!instance)
		return -1;

	for (i = 0; i < g_abcrypto_private_ctx.instances_count; i++) {
		cur = g_abcrypto_private_ctx.instances[i];
		if (cur) {
			if (strcmp(instance->name, cur->name) == 0) {
				fprintf(stderr,
					"Already exist instance named %s\n",
					cur->name);
				return -1;
			}
		}
	}

	if (g_abcrypto_private_ctx.instances_count >= ABCRYPTO_MAX_INSTANCES) {
		fprintf(stderr, "Limited instance num %d\n",
			ABCRYPTO_MAX_INSTANCES);
		return -1;
	}

	/* check callback... */
	g_abcrypto_private_ctx.instances[i] = instance;
	g_abcrypto_private_ctx.instances_count++;

	return 0;
}

void unregister_instance(struct abcrypto_instance *instance)
{
	/*unused*/
}

struct abcrypto_instance *abcrytpo_instance_get(const char *name)
{
	uint16_t i;

	for (i = 0; i < g_abcrypto_private_ctx.instances_count; i++) {
		if (g_abcrypto_private_ctx.instances[i]) {
			if (strcmp(name,
				   g_abcrypto_private_ctx.instances[i]->name) ==
			    0)
				return g_abcrypto_private_ctx.instances[i];
		}
	}

	return NULL;
}
